// ActionScript file
import flash.events.KeyboardEvent;

import mx.controls.Alert;
import mx.core.Application;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import vclass.Vhttp;

private function dologin():void{
	var my:Vhttp = new Vhttp();
	my.addEventListener(ResultEvent.RESULT,afisare);
	my.addEventListener(FaultEvent.FAULT,eroare);
	
	Application.application.removeEventListener(KeyboardEvent.KEY_DOWN,dologinOnEnter);
	my.target = null;
	my.url = domeniu+"login2.php";
	my.request.nume = username.text;
	my.request.pass = password.text;
	my.send();
}
private function dologinOnEnter(event:KeyboardEvent):void{
	if(event.keyCode == 13){
		Application.application.removeEventListener(KeyboardEvent.KEY_DOWN,dologinOnEnter);
		var my:Vhttp = new Vhttp();
		my.addEventListener(ResultEvent.RESULT,afisare);
		my.addEventListener(FaultEvent.FAULT,eroare);
		my.target = null;
		my.url = domeniu+"login2.php";
		my.request.nume = username.text;
		my.request.pass = password.text;
		my.send();
	}
}

private function afisare(evt:ResultEvent):void{
	var hehe:XML = new XML(evt.result);
	var flag:Number = hehe.attribute("flag");
	if(flag != 0){
		sesiune = hehe.row.attribute("session");
		nume = hehe.row.attribute("nume");
		this.currentState = hehe.row.attribute("cont");
	}else{
		username.text = "";
		password.text = "";
		loginForm.title = "Login error!";
		username.setFocus();
	}
	evt.target.removeEventListener(ResultEvent.RESULT,afisare);
	evt.target.removeEventListener(FaultEvent.FAULT,eroare);
}

private function eroare(evt:FaultEvent):void{
	Alert.show("Error comunication with server!");
	evt.target.removeEventListener(ResultEvent.RESULT,afisare);
	evt.target.removeEventListener(FaultEvent.FAULT,eroare);		
}

private function cldologout():void{
	this.currentState = 'login';
	sesiune = '';
	nume = '';
	//clNav.selectedIndex = 0;
}
private function addologout():void{
	this.currentState = 'login';
	sesiune = '';
	nume = '';
	//adNav.selectedIndex = 0;
}
private function clearForm():void{
		//reface listener la enter
		Application.application.addEventListener(KeyboardEvent.KEY_DOWN,dologinOnEnter);
		username.text = "";
		password.text = "";
}